package com.juvepoland.jpdroid.adapters;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.juvepoland.jpdroid.R;
import com.juvepoland.jpdroid.activities.NewsActivity;
import com.juvepoland.jpdroid.common.ImageLoader;
import com.juvepoland.jpdroid.managers.impl.NewsManagerImpl;
import com.juvepoland.jpdroid.models.News;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LazyNewsAdapter extends BaseAdapter {
    
    private Activity activity;
    private List<News> newsList;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    private Typeface basicFontTypeFace;
    private Typeface basicItalicFontTypeFace;
    private boolean displayImages;
    
    
    public LazyNewsAdapter(Activity a, List<News> list, Typeface basicFontTypeFace, Typeface basicItalicTypeface, boolean displayImages) {
        activity = a;
        this.basicFontTypeFace = basicFontTypeFace;
        this.basicItalicFontTypeFace = basicItalicTypeface;
        newsList=list;
        this.displayImages = displayImages;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView titleTextView = (TextView)vi.findViewById(R.id.title); //TODO: pozamieniac w xml-u nazwy kontrolek
        TextView dateTextView = (TextView)vi.findViewById(R.id.date); 
        
      
        titleTextView.setTypeface(basicFontTypeFace);
        dateTextView.setTypeface(basicFontTypeFace);
        
        //TextView commentsCountTextViev = (TextView)vi.findViewById(R.id.comments_count); 
       
        TextView id = (TextView) vi.findViewById(R.id.pid);
        
        News news = newsList.get(position);

        titleTextView.setText(news.getTitle());
     
        dateTextView.setText(news.getStringDate());
        //commentsCountTextViev.setText(String.valueOf(newsManager.getNewsCommentCnt(news)));
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); 
        if(displayImages){
        	 imageLoader.DisplayImage("http://www.juvepoland.com/images/news/"+news.getId()+".jpg", thumb_image);
        }
        else{
        	thumb_image.setVisibility(LinearLayout.GONE);
        }
       
        id.setText(String.valueOf(news.getId()));
        return vi;
    }
}